package com.wxy.servlet;

import com.wxy.model.Country;
import com.wxy.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/SelectMedalByIdServlet")
public class SelectMedalByIdServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Country> countryList = new ArrayList<Country>();
        String id = request.getParameter("id");
        Country  country = new Country();;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        conn = DBUtil.getConnection();
        String sql = "select * from nuc_country where id=?";
        try {

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,id);
            rs = pstmt.executeQuery();
            rs.next();
            country.setId(rs.getInt("id"));
            country.setCountry(rs.getString("country"));
            country.setGold(rs.getInt("gold"));
            country.setSilver(rs.getInt("silver"));
            country.setCopper(rs.getInt("copper"));
            countryList.add(country);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(rs,pstmt,conn);
        }

        request.getSession().setAttribute("country",countryList);
//        System.out.println(countryList);
        response.sendRedirect("alterMedal.jsp");
        response.setCharacterEncoding("utf-8");

    }
}
